// import SelectForm from './Select.form';
// import components from 'formiojs/components';
import Formio from 'formiojs/Formio';
import _ from 'lodash';
// import utils from '../../utils';
import Select from '../select/Select';

// import FormioUtils from 'formiojs/components/_classes/component/editForm/utils';

// const { formatCondition } = utils;
// const { select: Select } = components; // 仿佛不能用 import { xx } 的形式导出来

class SelectPro extends Select {
  constructor(path: any, options?: any, data?: any) {
    super(path, options, data);
  }

  static schema(...extend: any[]) {
    return Select.schema(
      {
        type: 'select',
        key: 'select',
        dymicQuery: [], // 获取数据的动态的条件参数 map
        // customConditionalInput: [], // 自定义的条件参数
      },
      ...extend,
    );
  }

  static get builderInfo() {
    return {
      title: 'Select',
      group: 'basic',
      icon: 'th-list',
      weight: 70,
      // documentation: '/userguide/#select',
      schema: SelectPro.schema(),
    };
  }

  get inputInfo() {
    const info = super.elementInfo();
    info.type = 'select';
    info.changeEvent = 'change';
    return info;
  }

  /*
   * 获取数据的地方
   */
  updateItems(searchInput: String | Number | null, forceUpdate: any) {
    let { url } = this.component.data;
    let { dataSrc } = this.component;
    let method;
    let body;

    // 如果配置的获取路径不是 url
    if (!url || dataSrc != 'url') return super.updateItems();

    // 获取动态的依赖数据
    const data: DymicQueryMap = this.root.data;
    const dymicQuery: DymicQuery[] = this.component.dymicQuery;
    let dymicDep: Object = {};

    if (!_.isEmpty(dymicQuery)) {
      // Add the query string.
      dymicDep =
        dymicQuery && dymicQuery.length
          ? dymicQuery.reduce((ret: DymicQueryMap, item: DymicQuery) => {
              let { key, value } = item;
              key = `${key}`;
              value = `${value}`;
              if (key && value) ret[key] = data[value];
              return ret;
            }, {})
          : {};
    }

    if (url.startsWith('/')) {
      // if URL starts with '/project', we should use base URL to avoid issues with URL formed like <base_url>/<project_name>/project/<project_id>/...
      const baseUrl = url.startsWith('/project')
        ? Formio.getBaseUrl()
        : Formio.getProjectUrl() || Formio.getBaseUrl();
      url = baseUrl + url;
    }

    if (!this.component.data.method) {
      method = 'GET';
      url += (!url.includes('?') ? '?' : '&') + Formio.serialize(dymicDep);
    } else {
      method = this.component.data.method;
      if (method.toUpperCase() === 'POST') {
        body = Object.assign({}, this.component.data.body, dymicDep);
      } else {
        body = null;
      }
    }

    // 强制取消缓存
    const options = this.component.authenticate
      ? { ignoreCache: true }
      : { noToken: true, ignoreCache: true };

    this.loadItems(
      url,
      searchInput,
      this.requestHeaders,
      options,
      method,
      body,
    );
  }

  // /*
  //  * 显示下拉框事件
  //  */
  // update() {
  //   // this.updateItems(null, false); // 每次下拉都重新获取数据
  //   super.update();
  // }

  render() {
    // 绑定 refreshOn
    const dymicQuery: DymicQuery[] = this.component.dymicQuery;

    if (!_.isEmpty(dymicQuery)) {
      this.component.refreshOn = dymicQuery
        .map((query) => query.value)
        .filter((i) => i != null); // 刷新源 = 动态参数配置
      this.component.clearOnRefresh = true; // 如果动态参数源有修改，记得要刷新本 select
      this.setValue(this.emptyValue); // 因为动态的条件，上次选择的值可能在这一次渲染的 options 里不存在，所以每次 render 都情况 value
    }
    return super.render();
  }
}

export default SelectPro;
